//格式化
function formatterProductName(value, row, index) {
    if (value) {
        return value.productName;
    }
}
function formatterEmpName(value, row, index) {
    if (value) {
        return value.name;
    }
}
function formatterShopName(value, row, index) {
    if (value) {
        return value.name;
    }
}


$(function () {
    //渲染表格
    var stockout_datagrid = $("#stockout_datagrid");
    var stockout_dialog = $("#stockout_dialog");
    var stockout_add_form = $("#stockout_add_form");


    stockout_datagrid.datagrid({
        url: "/stockoutcome/list",
        fit: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        columns: [[
            {field: "sn", title: "出货编号", width: 100},
            {field: "totalAmount", title: "总金额", width: 100},
            {field: "totalNumber", title: "总数量", width: 100},
            {field: "inputTime", title: "录入时间", width: 100},
            {field: "product", title: "商品名称", width: 100, formatter: formatterProductName},
            {field: "inputUser", title: "操作员", width: 100, formatter: formatterEmpName},
            {field: "shop", title: "店铺", width: 100, formatter: formatterShopName},
        ]],
        toolbar: "#stockoutcome_toolbar"
    })


    stockout_dialog.dialog({
        width: 600,
        height: 400,
        closed: true,
        onClose: function () {
            stockout_add_form.form('clear')
        }
    })

    var methodObj = {
        //高级查询
        search: function () {
            //1.获取查询input里面的值
            var keyword = $("#sreachs").val();
            //2.调用datagrid重新加载数据,并且带上查询条件  keyword
            stockout_datagrid.datagrid("load", {keyword: keyword});
        },
        add: function () {
            stockout_dialog.dialog('open');
            stockout_dialog.dialog('setTitle', '新增员工');
        },
        edit: function () {
            var row = stockout_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据', 'warning');
                return;
            }
            if(row.product){
                row['product.id'] = row.product.id;
            }
            if(row.inputUser){
                row['inputUser.id'] = row.inputUser.id;
            }
            if(row.shop){
                row['shop.id'] = row.shop.id;
            }
            stockout_add_form.form("load",row);
            stockout_dialog.dialog('open');

            stockout_dialog.dialog('setTitle', '编辑员工');
        },

        reload: function () {
            stockout_datagrid.datagrid('reload');
        },
        remove: function () {
            var selectedRow = stockout_datagrid.datagrid("getSelected");

            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "请选择一行数据", "error");
                return;
            }
            //给出提示确认要删除吗
            $.messager.confirm("提示", "您确定要删除吗?", function (yes) {
                if (yes) {

                    //发送ajax请求执行删除
                    $.post("/stockoutcome/delete", {id: selectedRow.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                methodObj.reload();
                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }
                    });
                }
            })
        },
        //取消
        cancel: function () {
            //关闭添加弹窗
            stockout_dialog.dialog("close");
        },

        //提交保存
        save: function () {
            var url = "/stockoutcome/save";
            //获取员工的id
            var stockoutcomeId = $("#stockout_id").val();
            if (stockoutcomeId) {
                //编辑的url
                url = "/stockoutcome/update";
            }
            //提交表单
            stockout_add_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            methodObj.reload();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    });
});